<?php

class Sendlog_Controller Extends Controller {

    private $type_object;
	private $sendstate;
	private $arealist;
	private $keeptime;	//保存时间
    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        $this->arealist=get_area_list_option();
        $this->type_object = get_type_object_db();
        //$this->type_object = get_type_object_id_name();
        $this->sendstate=array(
        		'0'=>'未知错误',
        		'1'=>'发送成功',
        		'2'=>'角色名找不到',
        		'3'=>'附件名字写错',
        		'4'=>'离线发送',
        );
        //$this->load->database('db');
		$this->db->select_db();
    }

    function index() {
        $this->view->display("mail/sendlog/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $rows = array();
        $sql = "SELECT COUNT(id) AS num FROM mail_sendlog WHERE {$where}";
        $one = $this->db->one($sql);

        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "ORDER BY id DESC";
        $sql = "SELECT {$colsinfo} FROM mail_sendlog WHERE {$where} {$orderby} LIMIT {$offset},{$rp}";
        $this->db->query($sql);

        while ($n = $this->db->get_array('default')) {
        	
        	$n['item0'] = $n['item0'] == '' ? '' : $this->type_object[$n['item0']] . "(".$n['item0'].")";
            $n['item1'] = $n['item1'] == '' ? '' : $this->type_object[$n['item1']] . "(".$n['item1'].")";
            $n['item2'] = $n['item2'] == '' ? '' : $this->type_object[$n['item2']] . "(".$n['item2'].")";
            $n['item3'] = $n['item3'] == '' ? '' : $this->type_object[$n['item3']] . "(".$n['item3'].")";
           
            $n['insert_time'] = get_datetime($n['insert_time']);
            $n['issuccess']=$this->sendstate[$n['issuccess']]."(".$n['issuccess'].")";
            $n['areanum']=$this->arealist[$n['areanum']];
            
            $rows[] = array('id' => $n['id'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }
	
    function del($id) {
    		
    	$c = 0;
    	$r = '';
    
    	if ($this->db->delete('mail_sendlog', array('id' => $id))) {
    		insert_log(translate('MENU_DATASTATISTICE_DEL'), array('id'=>$id));
    		$c = 1;
    		$r = translate('C_TIPS_SUCCESS_TEXT');
    
    	} else {
    		$r = translate('C_TIPS_ERROR_TEXT');
    	}
    	json_output($c, $r);
    
    }
    
    function multi() {
    
    	$type = $this->input->post("type");
    	$ids = $this->input->post("ids");
    	$c = 0;
    	$r = '';
    	if (count(explode(',', $ids)) == 0) {
    		$r = translate('C_INVALID_PARAMS');
    	} else {
    		if ($type == 'delete') {
    			$this->db->query("DELETE FROM mail_sendlog WHERE id IN ({$ids})");
    			$c = 1;
    		} else {
    			$r = translate('C_INVALID_PARAMS');
    		}
    	}
    	json_output($c, $r);
    		
    }

    function edit($id = 0) {
    	set_time_limit(0);
    
    	if (is_submit()) {
    		$this->send($_POST['id']);
    		return;
    	}
    	$row = $this->db->one("SELECT * FROM mail_sendlog WHERE id=$id");  
    	
  
    	$row['insert_time'] = get_datetime($row['insert_time']);
    	$row['issuccess']=$this->sendstate[$row['issuccess']]."(".$row['issuccess'].")";
    	$row['areanum']=$this->arealist[$row['areanum']];
    	
    	$row['item0'] = $row['item0'] == '' ? '' : $this->type_object[$row['item0']] . "(".$row['item0'].")";
    	$row['item1'] = $row['item1'] == '' ? '' : $this->type_object[$row['item1']] . "(".$row['item1'].")";
    	$row['item2'] = $row['item2'] == '' ? '' : $this->type_object[$row['item2']] . "(".$row['item2'].")";
    	$row['item3'] = $row['item3'] == '' ? '' : $this->type_object[$row['item3']] . "(".$row['item3'].")";
    		
    	$this->view->assign("id",$id);
    	$this->view->display("mail/sendlog/edit", array("row" => $row));
    }
    
    //群发记录
    function add()
    {
    	$type = $this->input->post("type");
    	$ids = $this->input->post("ids");
    	$c = 0;
    	$r = '';
    	echo " type = $type";
    	print_r($ids);
    	
    	if (count(explode(',', $ids)) == 0) {
    		$r = translate('C_INVALID_PARAMS');
    	} else {
    		if ($type == 'delete') {
    			$this->db->query("DELETE FROM mail_sendlog WHERE id IN ({$ids})");
    			$c = 1;
    		} else if('add' == $type)
    		{
    			
    		} 
    		else{
    			$r = translate('C_INVALID_PARAMS');
    		}
    	}
    	//json_output($c, $r);
    }
    
    //根据id，发送邮件
    function send($id)
    {
    	$row = $this->db->one("SELECT * FROM mail_sendlog WHERE id=$id");
    	if(0 == count($row))
    	{
    		echo ' invalid id $id ';
    		return ;
    	}
    	//得到区信息
    	$areanum = $row['areanum'];
    	$arealist = get_area_list_url();
    	
    	if (null == $arealist[$areanum] || ""==$arealist[$areanum]) {
    		echo " areanum error";
    		return;
    	}
    	
    	//发送前，去掉几个字段
    	unset($row['id']);
    	unset($row['issuccess']);
    	unset($row['areanum']);
    	unset($row['insert_time']);
    	
    	//因在发送前已经检查过数据了，在此不需要再检查数据的有效性了
    	$url = $arealist[$areanum]. array_to_stringparam($row)."action=15";
    	//echo " url=$url ";
    	 
    	$ret=  vpost($url);
    	//echo " ret = $ret url = $url";
    	if (1 === $ret) {
    		echo 'success';
    	}else if(2 == $ret){
    		echo 'can not find role name';
    	}else if(3 == $ret)
    	{
    		echo "item error,plz input again";
    	}else if( 0 == $ret)
    	{
    		echo 'unknow error1';
    	}else if( 4 == $ret)
    	{
    		echo 'offline send';
    	}else 
    	{
    		echo "unknow error2";
    	}
    	
    	$ret = $ret == "" ? 0:$ret;
    	
    	$ret = $this->db->update("mail_sendlog", array('issuccess'=>$ret,'insert_time'=>time()), array('id' => $id));
    	
    	
    }
}

?>